//
//  XLHomeAdapter.m
//  industry
//
//  Created by xl on 2020/11/25.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLHomeAdapter.h"
#import "XLHomeTitleHeaderView.h"
#import "XLHomeCell.h"
#import "XLHomeInteractorProtocol.h"

@implementation XLHomeAdapter

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else if (section == 1) {
        return self.commonArr.count;
    } else {
        return self.allArr.count;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(52, 80);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        CGSize size = KIsiPhonexPlus ? CGSizeMake(SCREEN_WIDTH, STATUSBAR_HEIGHT + 320 + 14) : CGSizeMake(SCREEN_WIDTH, STATUSBAR_HEIGHT + 320);
        return size;
    } else if (section == 1) {
        return (self.commonArr.count > 0) ? CGSizeMake(SCREEN_WIDTH, 45):CGSizeMake(0, 0);
    } else {
        return (self.allArr.count > 0) ? CGSizeMake(SCREEN_WIDTH, 45):CGSizeMake(0, 0);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
     return UIEdgeInsetsMake(0, 30, 0, 30);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10.0f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return floor((SCREEN_WIDTH - 30*2 - 52*4)/3.0);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 0) {
            self.headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XLHomeHeaderView" forIndexPath:indexPath];
            WEAKSELF
            _headerView.editBlock = ^{
                STRONGSELF
                XLSend(collectionView.baseController.interactor, XLHomeInteractorProtocol, gotoEditIndexVC:strongSelf.indexArr);

            };
            _headerView.switchBlock = ^{
                if (XL_UserInfoData.isStaff) {
                    XLSend(collectionView.baseController.interactor, XLHomeInteractorProtocol, gotoSwitchCompanyVC);
                } else {
                    XLSend(collectionView.baseController.interactor, XLHomeInteractorProtocol, gotoSwitchUserVC);
                }
            };
            _headerView.indexBlock = ^{
                STRONGSELF
                XLSend(collectionView.baseController.interactor, XLHomeInteractorProtocol, gotoAllIndexVC:strongSelf.indexArr);
            };
            // 跳转“扫一扫”页面
            _headerView.scanBlock = ^{
                XLSend(collectionView.baseController.interactor, XLHomeInteractorProtocol, gotoScanCodeVC);

            };
            [_headerView refreshUserInfo:XL_UserInfoData];
            [_headerView refreshIndexDatas:self.indexArr];
            return self.headerView;
        } else {
            XLHomeTitleHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XLHomeTitleHeaderView" forIndexPath:indexPath];
            if (indexPath.section == 1) {
                [headerView loadHeaderViewTitle:@"常用"];
            } else {
                [headerView loadHeaderViewTitle:@"全部应用"];
            }
            return headerView;
        }
    }
    return nil;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XLHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XLHomeCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        return [XLCollectionViewCell new];
    } else if (indexPath.section == 1){
        if (indexPath.row > self.commonArr.count - 1) {
            return [XLCollectionViewCell new];
        }
        [cell loadCellData:self.commonArr[indexPath.row]];
        return cell;
    } else {
        if (indexPath.row > self.allArr.count - 1) {
            return [XLCollectionViewCell new];
        }
        [cell loadCellData:self.allArr[indexPath.row]];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    id cellData;
    if (indexPath.section == 0) {
         return;
    } else if (indexPath.section == 1) {
        if (indexPath.row > self.commonArr.count - 1) {
            return;
        }
        cellData = self.commonArr[indexPath.row];
    } else {
        if (indexPath.row > self.allArr.count - 1) {
            return;
        }
        cellData = self.allArr[indexPath.row];
    }

    if (cellData != nil && self.adapterDelegate && [self.adapterDelegate respondsToSelector:@selector(didSelectCellData:)]) {
        [self.adapterDelegate didSelectCellData:cellData];
    }
}

@end
